package com.ruoyi.flow.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.flow.domain.bo.TWorkflowBo;
import com.ruoyi.flow.domain.vo.TWorkflowVo;
import com.ruoyi.flow.service.ITWorkflowService;
import com.ruoyi.operation.api.RemoteExecuteSidService;
import com.ruoyi.operation.api.vo.ServiceMap;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;

import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.servlet.http.HttpServletResponse;

/**
 * 工作流控制器
 * 前端访问路由地址为:/workflow/workflow
 *
 * @author zzneko
 * @date 2025-07-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/workflow")
public class TWorkflowController extends BaseController {

    private final ITWorkflowService iTWorkflowService;
    @DubboReference
    private RemoteExecuteSidService remoteExecuteSidService;

    /**
     * 查询工作流列表
     */
    @SaCheckPermission("workflow:workflow:list")
    @GetMapping("/list")
    public TableDataInfo<TWorkflowVo> list(TWorkflowBo bo, PageQuery pageQuery) {
        return iTWorkflowService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出工作流列表
     */
    @SaCheckPermission("workflow:workflow:export")
    @Log(title = "工作流", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TWorkflowBo bo, HttpServletResponse response) {
        List<TWorkflowVo> list = iTWorkflowService.queryList(bo);
        ExcelUtil.exportExcel(list, "工作流", TWorkflowVo.class, response);
    }

    /**
     * 获取工作流详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("workflow:workflow:query")
    @GetMapping("/{id}")
    public R<TWorkflowVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(iTWorkflowService.queryById(id));
    }

    /**
     * 新增工作流
     */
    @SaCheckPermission("workflow:workflow:add")
    @Log(title = "工作流", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TWorkflowBo bo) {
        return toAjax(iTWorkflowService.insertByBo(bo));
    }

    /**
     * 修改工作流
     */
    @SaCheckPermission("workflow:workflow:edit")
    @Log(title = "工作流", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TWorkflowBo bo) {
        return toAjax(iTWorkflowService.updateByBo(bo));
    }

    /**
     * 删除工作流
     *
     * @param ids 主键串
     */
    @SaCheckPermission("workflow:workflow:remove")
    @Log(title = "工作流", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iTWorkflowService.deleteWithValidByIds(Arrays.asList(ids), true));
    }


    @Log(title = "审批", businessType = BusinessType.OTHER)
    @PostMapping("/audit")
    @Transactional(rollbackFor = Exception.class)
    public R<Void> handle(@RequestBody TWorkflowBo tWorkflowBo, Long taskId, String skipType, String message
        , String nodeCode, String flowStatus) {
        return toAjax(iTWorkflowService.handle(tWorkflowBo, taskId, skipType, message, nodeCode, flowStatus));
    }

    @GetMapping("/service/map")
    public R<List<ServiceMap>> getServiceMap(@RequestParam String service){
        List<ServiceMap> serviceMap = remoteExecuteSidService.getServiceMap(service);
        List<String> executedServiceId = iTWorkflowService.queryList(new TWorkflowBo()).stream().map(TWorkflowVo::getServiceId).collect(Collectors.toList());
        List<ServiceMap> resultList = serviceMap.stream()
            .filter(x -> !executedServiceId.contains(x.getServiceId()))
            .collect(Collectors.toList());
        return R.ok(resultList);
    }

}
